package main

import (
	"fmt"
	"math/rand"
	"time"
)

type RAND struct {
	r           *rand.Rand
	base        float64
	num         int
	slice       []float64
	resultSlice []float64
}

func (r *RAND) Init() {
	r.r = rand.New(rand.NewSource(time.Now().Unix()))
	r.base = 1e5
	r.num = 100
	r.slice = make([]float64, r.num)
	r.resultSlice = make([]float64, r.num)
	for i := 0; i < r.num; i++ {
		r.slice[i] = float64(r.r.Int63n(500) + 100)
	}
	// r.slice[r.num-1] = 1e5
}

func (r *RAND) Result() {
	sum := 0.0
	for i := 0; i < r.num; i++ {
		sum += r.slice[i]
	}
	sum1 := sum
	sum2 := 0.0
	for i := 0; i < r.num; i++ {
		b := (r.base + sum) * (-1.1 + float64(r.r.Intn(10))/float64(10) + r.slice[i]*float64(r.num)/sum) / float64(r.num)
		if b > 0.0 && sum > 0.0 {
			sum -= b
		} else {
			b = 0.0
		}
		sum2 += b
		r.resultSlice[i] = b
	}
	for i := 0; i < r.num; i++ {
		fmt.Printf("%f\t%f\n", r.slice[i], r.resultSlice[i])
	}
	fmt.Printf("%f\n%f\n%f\n", sum1, sum2, sum)
}

func main() {
	var r RAND
	r.Init()
	r.Result()
}
